<?php
/*******************************************************************************
login.php

Created by Maurice York (maurice_york@ncsu.edu)
Modified by NCSU Libraries, NC State University. Modifications by Karl Doerr, Troy Hurteau, Jason Raitz(libraries.opensource@ncsu.edu).

This file is part of NCSU's distribution of ReservesDirect. This version has not been downloaded from Emory University
or the original developers of ReservesDirect. Neither Emory University nor the original developers of ReservesDirect have authorized
or otherwise endorsed or approved this distribution of the software.

Copyright (c) 2004-2006 Emory University, Atlanta, Georgia.

Licensed under the NCSU ReservesDirect License, Version 2.0 (the "License"); 
you may not use this file except in compliance with the License. You may obtain a copy of the full License at
 http://www.lib.ncsu.edu/it/opensource/

ReservesDirect is distributed in the hope that it will be useful,
but is distributed "AS IS" and WITHOUT ANY WARRANTY, without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE, and without any warranty as to non-infringement of any third
party's rights. See the License for the specific language governing permissions and limitations under the License.

The original version of ReservesDirect is located at:
http://www.reservesdirect.org/

This version of ReservesDirect, distributed by NCSU, is located at:
http://code.google.com/p/reservesdirect-ncsu/

*******************************************************************************/
//$debugMode = true;
//$errorLevel = E_ALL; //0 for none or E_ALL for all
# TODO: needed still?
if($debugMode){
	error_reporting(E_ALL);
	ini_set('display_errors', 'On');
}

$mobile = false;

$loginOnly = array_key_exists('cmd', $_GET);
if($loginOnly){
	//#TODO make a sanitize utility
	try{
		$actionLabel = Rd_Dictionary::get('actions:' . $_GET['cmd'] . ':userLabel');
		$moreSpecificAction = htmlentities(" to {$actionLabel}");
	} catch (Exception $e) {
		$moreSpecificAction = ' to Proceed';
	}
} else {
	$moreSpecificAction = '';
}
//#TODO this needs to be pulled into a utility class... and not included in the OS release since it is campus specific
// Detect mobile device. If mobile (or if "mobile=true" is included in URL query string), sets $mobile to true.
// Variable is passed into login form to be available to next screen
if ($_SERVER['HTTP_USER_AGENT'] && !(array_key_exists('mobile', $_REQUEST) && $_REQUEST['mobile'] == 'true')) {
	$url = 'http://webapps.ncsu.edu/wurfl/?method=getDeviceByUserAgent&userAgent=' . urlencode($_SERVER['HTTP_USER_AGENT']) . '&capabilities=mobile_browser,model_name,device_os';

	// Initialize the CURL library
	$ch = curl_init();

	// Set the URL to execute
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_TIMEOUT, 180);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_USERAGENT, "NCSU Libraries Reserves Direct Mobile");
	$result = curl_exec($ch);
	curl_close($ch);

	if ((isset($type) && ($type == 'smart_phone' || $type == 'iphone')) || (array_key_exists('mobile', $_REQUEST) && $_REQUEST['mobile'] == 'true')) {
		$mobile = true;
	}
	else {
		$mobile = false;
	}
} else if (array_key_exists('mobile', $_REQUEST) && $_REQUEST['mobile'] == 'true'){
	$mobile = true;
}


if ($mobile){
?>
<html>
	<head>
		<title><?php print(Rd_Registry::get('instanceName')); ?></title>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
		<meta name="viewport" content="width=device-width, user-scalable=no">
		
		<link rel="apple-touch-icon" sizes="57x57" href="public/images/mobile/homescreen-icon-57x57.png" />
			<link rel="apple-touch-icon" sizes="114x114" href="public/images/mobile/homescreen-icon-114x114.png" />
			<link rel="apple-touch-icon-precomposed" href="public/images/mobile/homescreen-icon-57x57.png"/>
		  	<link rel="stylesheet" href="public/css/jquery.mobile-1.0.min.css" />
		    <link rel="stylesheet" href="public/themes/general-mobile-theme.min.css" />
		    
		<link rel="stylesheet" href="css/general-mobile.css" type="text/css">
		<script type="text/javascript" src="public/javascript/jquery-1.6.4.min.js"></script>
			<script>
				$(document).bind("mobileinit", function(){
					$.mobile.ajaxEnabled = false;
					$.mobile.pushStateEnabled = false;
					$.mobile.hashListeningEnabled = false;
					//$.mobile.attachEvents = disabled;
				});
			</script>
		<script src="public/javascript/jquery.mobile-1.0.min.js"></script>
		
	</head>
	
	<body id="mobileBody">

		<div id="container" data-role="page" data-theme="a">
			
			<!--Mobile banner-->
			<?php include(APPLICATION_PATH . '/html/bannerM.inc.html'); ?>
			
			<!--  RD displayer and main div -->
		    <div id="content" data-role="content" class="reducedMargin loginForm">
		    
		    	<p><b>Login Required</b>
		    	<i>Please enter your User ID and password</i></p>
				<form name="RDlogin" method="post" action="<?php print(Rd_UrlRewrite::scrubGet($_SERVER['REQUEST_URI'], $_GET, array('exclude'=>array('failureCode')))); ?>">
				  <?php if(isset($login_error) && '' != $login_error): ?>
					<p class="reducedMargin"><span class="smallprint"><font color="red"><?php print($login_error); ?></font></span></p>
				  <?php endif; ?>
					<p><strong>Username:</strong>
						<input name="username" id="user_name" type="text" size="15" autocapitalize="off" autocorrect="off" data-theme="c" /></p> 
					<p><strong>Password:</strong>
						<input name="pwd" type="password" size="15" data-theme="c" /></p>
						<input type="hidden" name="mobile" value="true" />
						<input data-ajax="false" name="submitForm" type="submit" value="Sign In" />

				</form>
		    </div>

			<!-- Footer for bailout to full page -->
			<?php include(APPLICATION_PATH . '/html/footerM.inc.html'); ?>
		</div>
	
	</body>
</html>
<?php 
} else {
?>
	<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
	<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
	
	<!-- METADATA: insert content from stage/webpub/metadata form below this line -->
	<title><?php print(Rd_Registry::get('instanceName')); ?> - Login</title>
	
	<script language="JavaScript1.2" src="secure/javascript/jsFunctions.js"></script>
	
	<style type="text/css" media="screen">
		/*
		******************************************
		Login
		******************************************
		*/
		
		.centeredLogin {margin-left:auto;margin-right:auto;width:25em;}
	</style>
	
	</head>
	<body onload="var elmt = document.getElementById('user_name'); if(elmt) { elmt.focus(); } else { focusOnForm(); }">
		
	<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
	  <tr valign="top">
	    <td id="mainContent" style="padding: 0px;">
	
	      <table width="100%" border="0" cellspacing="0" cellpadding="0">
	        <tr valign="top">
	          <td id="mainContentTextServices" style="padding: 0px;">
	
	<!-- TITLE OF PAGE: enter title of page -->
	<h1 class="loginTitle"><?php print(Rd_Registry::get('instanceName')); ?></h1>
	<!-- BEGIN CONTENT -->
	
	<table<?php print($loginOnly ? ' class="centeredLogin"' : ''); ?>>
	  <tr>
	    <td align="left" width="40%">
		
		  <span class="loginTitle" style="padding-bottom:5px;"><font face="verdana, arial, helvetica, sans-serif" size=4><b>Login Required</b></font><br/></span>
		  <font face="verdana, arial, helvetica, sans-serif" size=2 class="loginTitle"><i>Please enter your User ID and password</i></font><br /> 
	
	   
		<table width="100%">
		  <tr>
		    <td align="center">
		
		      <font face="verdana, arial, helvetica, sans-serif" size=2 class="login">
		  	    
	      
		        <form name="RDlogin" method="post" action="<?php print(Rd_UrlRewrite::scrubGet($_SERVER['REQUEST_URI'], $_GET, array('exclude'=>array('failureCode')))); ?>" style="margin:0px;">
	                <table width="100%" border="0" align="center" cellpadding="5" cellspacing="0" class="loginBox" height="44">
	                  <tr valign="top">
	                    <td width="11%" height="10">&nbsp; </td>
	                    <td width="75%">
	                      <div id="loginText">
						  <?php if(isset($login_error) && '' != $login_error): ?>
						  	<font color="red"><?php print($login_error); ?></font>
						  <?php endif; ?>
						  <p><strong>Username:</strong>
	                          <input name="username" id="user_name" type="text" size="15" autocapitalize="off" autocorrect="off" class="loginInput"></p> 
	                          
	                          <p><strong>Password:&nbsp;
	                          <input name="pwd" type="password" size="15" class="loginInput">
	                        </strong> </p></div>
	                    </td>
	                    <td width="14%">&nbsp;</td>
	                  </tr>
	                  <tr valign="top">
	                    <td height="34" colspan="3" align="center">
		                  <input type="hidden" name="mobile" value="false" />
	                      <input name="submitForm" type="submit" value="Sign In<?php print($moreSpecificAction); ?>" style="<?php print( $loginOnly ? '' : 'width:8em; '); ?>height:3em;">
	                    </td>
	                  </tr>
	                </table>
	                </form>
	  		  </font>

			  <br />
			  	<div class="mobileNote">
			  		<?php print(Rd_Registry::get('instanceName')); ?> Mobile
			  	</div>
			  	<div class="mobileNote">
					<a href="index.php?mobile=true">Log in here</a>
				</div>
	        </td>
	
	      </tr>
	    </table>  			
	  			
	  	  
		 <!--  <br clear="all"/>  --> 
		 <br/>
		</td>
		<?php if (!$loginOnly): ?>
	        <td align="left" valign="top" width="60%" bgcolor="#EEEEEE" cellpadding="1" cellspacing="1">
				<div style="padding: 5px;">
	               Side Panel Content Here
	 			</div>
	        </td>
	     <?php endif;?>
	  </tr>
	</table>
	
	          </div></td>
	        </tr>
	      </table>
	    </td>
	    <td class="shadowRight"><img src="/website/images/clear.gif" width="3" height="1" alt=" " /></td>
	  </tr>
	  <tr>
	    <td class="shadowBottom"><img src="/website/images/clear.gif" width="1" height="4" alt=" " /></td>
	    <td><img src="/website/images/clear.gif" width="1" height="4" alt=" " /></td>
	  </tr>
	</table>
	</body>
	</html>
<?php 
}